package iris_controller

import (
	"fmt"
	"github.com/kataras/iris/v12"
	"gluenet/modules/manager/server/domain"
	"gluenet/modules/manager/server/domain/entity"
	"gluenet/modules/manager/server/domain/vo"
	"gluenet/modules/manager/server/infra"
	"gluenet/modules/manager/server/infra/prepare"
	"mime/multipart"
	"strings"
)

func init() {
	prepare.RegisterAuthController(func() {
		controller := NewConfigController()
		prepare.InjectAuthController(func() *Config {
			return controller
		})
	})
}

type Config struct {
	Config *domain.Config
}

func NewConfigController() *Config {
	r := &Config{}
	prepare.FetchDependency(&r.Config)
	return r
}

// Get serves
// Method:   GET
// Resource: http://localhost:8080/apis/config/all
func (c *Config) GetAll(ctx iris.Context) *infra.Response {
	id := ctx.Values().GetString("user_id")
	admin, err := ctx.Values().GetBool("admin")
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	objs, err := c.Config.FindByUser(id, admin, map[string]interface{}{})
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(objs)
}

// Get serves
// Method:   GET
// Resource: http://localhost:8080/apis/config/query
func (c *Config) GetQuery(ctx iris.Context) *infra.Response {
	params := ctx.URLParams()
	var query = map[string]interface{}{}
	for k, v := range params {
		query[k] = v
	}

	id := ctx.Values().GetString("user_id")
	admin, err := ctx.Values().GetBool("admin")
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}

	cfg, err := c.Config.FindByUser(id, admin, query)
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(cfg)
}

// Create serves
// Method:   POST
// Resource: http://localhost:8080/apis/config/create
func (c *Config) PostCreate(ctx iris.Context) *infra.Response {

	id := ctx.Values().GetString("user_id")
	admin, err := ctx.Values().GetBool("admin")
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}

	contentType := ctx.GetContentTypeRequested()

	var req = vo.CreateConfigReq{}
	var file multipart.File
	var fileName string
	if strings.Contains(contentType, "application/json") {
		if err := ctx.ReadJSON(&req); err != nil {
			return infra.ResponseReqErr(fmt.Errorf("read req %v", err), nil)
		}
	} else {
		req.Platform = entity.PlatformKubernetes
		fileUploads, info, err := ctx.FormFile("file")
		if err != nil {
			return infra.ResponseReqErr(fmt.Errorf("read file %v", err), nil)
		}
		file = fileUploads
		fileName = info.Filename
		if file == nil {
			fmt.Printf("1 file is nil", file)
		}
		defer file.Close()
	}

	req.UserID = id
	req.IsAdmin = admin
	req.FileName = fileName
	req.FileReader = file

	cfg, err := c.Config.Create(req)
	if err != nil {
		return infra.ResponseReqErr(fmt.Errorf("create file %v", err), nil)
	}
	return infra.ResponseOK(cfg)
}

// Get serves
// Method:   DELETE
// Resource: http://localhost:8080/apis/config/del
func (c *Config) DeleteDel(ctx iris.Context) *infra.Response {
	configId := ctx.URLParam("id")

	userId := ctx.Values().GetString("user_id")
	admin, err := ctx.Values().GetBool("admin")
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	_, err = c.Config.Delete(admin, userId, configId)
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(nil)
}
